package com.demo.utils;

import com.google.common.cache.*;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.demo.pojo.User;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkNotNull;

public class UserCacheUtil {
    private static Logger logger = LoggerFactory.getLogger(UserCacheUtil.class);
    public static RemovalListener<String, User> listener=new RemovalListener<String, User>() {
        @Override
        public void onRemoval(RemovalNotification<String, User> notification) {
            logger.info("缓存被移除");
        }
    };
    public static Cache<Integer,User> cache=CacheBuilder.newBuilder()
                .expireAfterWrite(60,TimeUnit.SECONDS)
                .maximumSize(100)
                .build();


    public static LoadingCache<Integer, User> loadingCache= CacheBuilder
            .newBuilder()
//            .removalListener(new RemovalListener<Integer, User>() {
//                @Override
//                public void onRemoval(RemovalNotification<Integer, User> notification) {
//                    logger.info("缓存被移除");
//                }
//            })
            .expireAfterWrite(60, TimeUnit.SECONDS)
            .maximumSize(100)
            .build(new CacheLoader<Integer, User>() {
                @Override
                public User load(Integer s) throws Exception {
                    User user=new User();
                    user.setUserid(s);
                    user.setUsername("默认");
                    return user;
                }
                @Override
                public ListenableFuture<User> reload(Integer key, User oldValue) throws Exception {
                    logger.info("刷新缓存");
                    checkNotNull(key);
                    checkNotNull(oldValue);
                    return Futures.immediateFuture(load(key));
                }

            });

    public static User getUserFromCache(int id) throws ExecutionException {
        return loadingCache.get(id);
    }

    public static void insertCache(User user) throws ExecutionException {
        logger.info("cache 大小：{}",loadingCache.size());;
        loadingCache.put(user.getUserid(),user);
    }

    public static void onFresh(int id) throws ExecutionException {
        logger.info("cache 大小：{}",loadingCache.size());;
        logger.info("onFresh刷新缓存");
        loadingCache.refresh(id);
    }

    public static User getUserFromCache1(int id) throws ExecutionException {
        return cache.get(id, new Callable<User>() {
            @Override
            public User call() throws Exception {
                User user=new User();
                user.setUserid(id);
                user.setUsername("默认");
                return user;
            }
        });
    }

    public static void insertCache1(User user) throws ExecutionException {
        logger.info("cache 大小：{}",cache.size());;
        cache.put(user.getUserid(),user);
    }
}
